package com.css.bjdt_core.cell.service;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.nutz.dao.Dao;
import org.nutz.dao.Sqls;
import org.nutz.dao.sql.Sql;
import org.nutz.dao.sql.SqlCallback;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.css.bjdt_core.cell.dao.CellDataDao;
import com.css.bjdt_core.cell.entity.Cell;
import com.css.bjdt_core.cell.entity.Cell.CellType;
import com.css.bjdt_core.cell.entity.CellData;
import com.css.bjdt_core.cell.entity.VarParam;
import com.css.bjdt_core.dnDzz.service.DnDzzService;
import com.css.bjdt_core.reportPrepare.entity.RptUnit;
import com.css.bjdt_core.reportPrepare.service.RptUnitService;
import com.css.bjdt_core.util.CommonUtil;
import com.css.bjdt_core.util.ReportConstant;

@Service
public class CellDataService extends CustomSqlService {
	@Autowired
	private Dao dao;
	@Autowired
	private CellDataDao cellDataDao;
	@Autowired
	private RptUnitService rptUnitService;
	@Autowired
	private CellService cellService;
	@Autowired
	private DnDzzService dnDzzService;

	/**
	 * 按年度id、党组织id、报表id获取相关指标数据
	 * 
	 * @param annualId   年度id
	 * @param unitId     党组织id
	 * @param templateId 报表id
	 * @return
	 */
	public List<CellData> getCellData(String annualId, String unitId, String templateId) {
		List<CellData> list = cellDataDao.selectCellData(annualId, unitId, templateId, null, null, null);
		if (list != null && !list.isEmpty()) {
			List<CellData> sumList = new ArrayList<>();
			for (CellData data : list) {
				if ("1".equals(data.getSumStatus())) {
					sumList.add(data);
				}
			}
			if (!sumList.isEmpty()) {
				return sumList;
			}
		}
		return list;
	}

	/**
	 * 按年度id、党组织id、单元格id获取相关指标数据
	 * 
	 * @param annualId 年度id
	 * @param unitId   党组织id
	 * @param cellIds  单元格id
	 * @return 返回指标值，如果指标没有值，则不返回
	 */
	public List<CellData> getCellDataByCellIds(String annualId, String unitId, String[] cellIds) {
		List<CellData> list = cellDataDao.selectCellData(annualId, unitId, null, cellIds, null, null);
		if (list != null && !list.isEmpty()) {
			List<CellData> sumList = new ArrayList<>();
			for (CellData data : list) {
				if ("1".equals(data.getSumStatus())) {
					sumList.add(data);
				}
			}
			if (!sumList.isEmpty()) {
				return sumList;
			}
		}
		return list;
	}

	/**
	 * 按年度id、党组织id、单元格id获取相关指标数据
	 * 
	 * @param annualId 年度id
	 * @param unitId   党组织id
	 * @param cellIds  单元格id
	 * @return 返回指标值，如果指标没有值(没有记录)，则new一个CellData返回
	 */
	public List<CellData> getAllCellDataByCellIds(String annualId, String unitId, String[] cellIds) {
		List<CellData> list = getCellDataByCellIds(annualId, unitId, cellIds);
		List<String> existIds = new ArrayList<>();

		for (CellData data : list) {
			existIds.add(data.getCellId());
		}

		for (String cellId : cellIds) {
			if (!existIds.contains(cellId)) {
				CellData cellData = new CellData();
				cellData.setCellId(cellId);
				cellData.setCellValue("0");
				cellData.setCellType("100");
				list.add(cellData);
			}
		}
		return list;
	}

	public boolean isFuncExist(String templateId) {
		String count = executeSql(ReportConstant.ISEXIST_FUNCTION_SQL, Sqls.callback.str(),
				new VarParam(null, null, "func_name", "QC_" + CommonUtil.uuidShorter(templateId))).getString();
		return !"0".equals(count);
	}

	public List<String> isFuncExist(List<String> templateIds) {
		Map<String, String> mapping = new HashMap<>();
		for (String templateId : templateIds) {
			mapping.put("QC_" + CommonUtil.uuidShorter(templateId), templateId);
		}
		Map<String, Object> vars = new HashMap<String, Object>();
		vars.put("object_names", CommonUtil.convertIDList(mapping.keySet()));
		List<String> objects = (List<String>) executeSql(ReportConstant.ISEXIST_FUNCTIONS_SQL, new SqlCallback() {
			@Override
			public Object invoke(Connection conn, ResultSet rs, Sql sql) throws SQLException {
				List<String> res = new ArrayList<String>();
				while (rs.next()) {
					res.add(rs.getString("object_name"));
				}
				return res;
			}
		}, null, vars).getResult();

		List<String> res = new ArrayList<String>();
		for (String object : objects) {
			res.add(mapping.get(object));
		}
		return res;
	}

	/**
	 * 动态创建指标数据表
	 *
	 * @param dzzId
	 */
	public Map<String, String> getCellDataByFunc(String templateId, String dzzId, String startTime, String endTime) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("dzzId", dzzId);
		params.put("startTime", startTime);
		params.put("endTime", endTime);
		Map<String, Object> vars = new HashMap<String, Object>();
		vars.put("funcName", "QC_" + CommonUtil.uuidShorter(templateId));
		return (Map<String, String>) executeSql(ReportConstant.EXECUTIVE_QUERY_BY_FUNC, new SqlCallback() {
			@Override
			public Object invoke(Connection conn, ResultSet rs, Sql sql) throws SQLException {
				Map<String, String> res = new HashMap<String, String>();
				while (rs.next()) {
					res.put(rs.getString("cell_id"), rs.getString("cell_value"));
				}
				return res;
			}
		}, params, vars).getResult();
	}

	public Map<String, List<CellData>> buildInputCellDataMapGroupByTemplateId(String annualId, String unitId) {
		List<CellData> cellDatas = cellDataDao.selectCellData(annualId, unitId, null, null, CellType.input.getKey(),
				null);
		Map<String, List<CellData>> map = new HashMap<String, List<CellData>>();
		if (cellDatas != null && cellDatas.size() > 0) {
			for (CellData cellData : cellDatas) {
				if (!map.containsKey(cellData.getTemplateId())) {
					map.put(cellData.getTemplateId(), new ArrayList<CellData>());
				}
				map.get(cellData.getTemplateId()).add(cellData);
			}
		}
		return map;
	}

	public int countCellDataByTemplate(String annualId, String unitId, String templateId) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("dzzId", unitId);
		params.put("templateId", templateId);
		Map<String, Object> vars = new HashMap<String, Object>();
		vars.put("tableName", ReportConstant.EXECUTIVE_TABLE_PREFIX + annualId);

		String sql = "SELECT COUNT(1) FROM $tableName cd, " + " (SELECT b.TEMPLATE_ID, b.CELL_ID FROM rpt_cell b WHERE "
				+ " b.TEMPLATE_ID = @templateId AND b.cell_type IN ('1', '2', '3') ) c "
				+ " WHERE cd.CELL_ID = c.CELL_ID AND cd.DZZ_ID = @dzzId ";

		return executeSql(sql, Sqls.callback.integer(), params, vars).getInt();
	}

	public Map<String, Integer> batchCountCellData(String annualId, String unitId) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("dzzId", unitId);
		Map<String, Object> vars = new HashMap<String, Object>();
		vars.put("tableName", ReportConstant.EXECUTIVE_TABLE_PREFIX + annualId);

		String sql = "SELECT c.TEMPLATE_ID, COUNT (1) AS con FROM $tableName cd, "
				+ " (SELECT b.TEMPLATE_ID, b.CELL_ID FROM rpt_cell b WHERE " + " b.cell_type IN ('1', '2', '3') ) c "
				+ " WHERE cd.CELL_ID = c.CELL_ID AND cd.DZZ_ID = @dzzId GROUP BY c.TEMPLATE_ID ";

		return (Map<String, Integer>) executeSql(sql, new SqlCallback() {
			@Override
			public Object invoke(Connection conn, ResultSet rs, Sql sql) throws SQLException {
				Map<String, Integer> res = new HashMap<String, Integer>();
				while (rs.next()) {
					res.put(rs.getString("TEMPLATE_ID"), rs.getInt("CON"));
				}
				return res;
			}
		}, params, vars).getResult();
	}

	public Map<String, Integer> batchCountCellDataByTemplates(String annualId, String unitId,
			Collection<String> templateIds) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("dzzId", unitId);
		Map<String, Object> vars = new HashMap<String, Object>();
		vars.put("tableName", ReportConstant.EXECUTIVE_TABLE_PREFIX + annualId);
		vars.put("templateIds", CommonUtil.convertIDList(templateIds));

		String sql = "SELECT c.TEMPLATE_ID, COUNT (1) AS con FROM $tableName cd, "
				+ " (SELECT b.TEMPLATE_ID, b.CELL_ID FROM rpt_cell b WHERE "
				+ " b.TEMPLATE_ID in ( $templateIds ) AND b.cell_type IN ('1', '2', '3') ) c "
				+ " WHERE cd.CELL_ID = c.CELL_ID AND cd.DZZ_ID = @dzzId GROUP BY c.TEMPLATE_ID ";

		return (Map<String, Integer>) executeSql(sql, new SqlCallback() {
			@Override
			public Object invoke(Connection conn, ResultSet rs, Sql sql) throws SQLException {
				Map<String, Integer> res = new HashMap<String, Integer>();
				while (rs.next()) {
					res.put(rs.getString("TEMPLATE_ID"), rs.getInt("CON"));
				}
				return res;
			}
		}, params, vars).getResult();
	}

	public Map<String, Integer> batchCountCellDataByCells(String annualId, String unitId, List<String> cellIds) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("dzzId", unitId);
		Map<String, Object> vars = new HashMap<String, Object>();
		vars.put("tableName", ReportConstant.EXECUTIVE_TABLE_PREFIX + annualId);
		vars.put("cellIdList", CommonUtil.convertIDList(cellIds));

		String sql = "SELECT c.OCID, COUNT(1) as con " + "FROM $tableName cd, (select a.OCID, b.CELL_ID from (SELECT "
				+ "template_id, CELL_ID as OCID FROM rpt_cell WHERE " + "cell_id in ( $cellIdList )) a, rpt_cell b "
				+ "where a.TEMPLATE_ID = b.TEMPLATE_ID and b.cell_type IN ('1', '2', '3')) c "
				+ "where cd.CELL_ID = c.CELL_ID and cd.DZZ_ID = @dzzId GROUP BY c.OCID ";

		return (Map<String, Integer>) executeSql(sql, new SqlCallback() {
			@Override
			public Object invoke(Connection conn, ResultSet rs, Sql sql) throws SQLException {
				Map<String, Integer> res = new HashMap<String, Integer>();
				while (rs.next()) {
					res.put(rs.getString("OCID"), rs.getInt("CON"));
				}
				return res;
			}
		}, params, vars).getResult();
	}

	/*public int countCellDataByTemplate(String annualId, String unitId, String cellId) {
		return cellDataDao.countCellDataByTemplate(annualId, unitId, cellId);
	}*/

	/**
	 * 删除
	 * 
	 * @param annualId   年度id
	 * @param dzzId      党组织id
	 * @param templateId 报表id
	 */
	public void deleteCellData(String annualId, String dzzId, String templateId) {
		cellDataDao.deleteCellDataByDzzIdAndTemplateId(annualId, dzzId, templateId);
	}

	/**
	 * 删除
	 * 
	 * @param annualId   年度id
	 * @param dzzId      党组织id
	 * @param templateId 报表id
	 * @param sumStatus  汇总状态
	 */
	public void deleteSumCellData(String annualId, String dzzId, String templateId, String sumStatus) {
		cellDataDao.deleteSumCellDataByDzzIdAndTemplateId(annualId, dzzId, templateId, sumStatus);
	}

	// 根据党组织删除该党组织下所有的报表数据
	public void deleteAllCellDataByDzzId(String annualId, String dzzId) {
		cellDataDao.deleteCellDataByDzzId(annualId, dzzId);
	}

	// 根据党组织删除该党组织下所有的报表汇总数据
	public void deleteAllCellSummaryDataByDzzId(String annualId, String dzzId) {
		cellDataDao.deleteCellSummaryDataByDzzId(annualId, dzzId);
	}

	/**
	 * 按单元格类型删除(只删除统计类型、计算类型、引用类型指标)
	 * 
	 * @param annualId 年度id
	 * @param unitId   党组织id
	 */
	public void deleteCellDataByCellType(String annualId, String unitId) {
		cellDataDao.deleteCellDataByCellType(annualId, unitId);
	}

	/**
	 * 按单元格类型删除(只删除统计类型、计算类型、引用类型指标)高杨新加的
	 * 
	 * @param annualId   年度id
	 * @param dzzId     党组织id
	 * @param templateIdList 报表id
	 */
	public void deleteCellDataByDzzIdAndTemplateIdGy(String annualId, String dzzId, List<String> templateIdList) {
		cellDataDao.deleteCellDataByDzzIdAndTemplateIdGy(annualId, dzzId, templateIdList);
	}

	/**
	 * 汇总
	 * 
	 * @param annualId 年度id
	 * @param cellId   单元格id
	 * @param unitIds  党组织id
	 * @return
	 */
	public String sumCellData(String annualId, String cellId, String[] unitIds) {
		return cellDataDao.sumCellData(annualId, cellId, unitIds);
	}

	/**
	 * 批量插入(性能不是最优)
	 * 
	 * @param annualId  年度id
	 * @param cellDatas 单元格数据集合
	 */
	public void batchInsertCellData(String annualId, List<CellData> cellDatas) {
		if (cellDatas != null && cellDatas.size() > 0) {
			List<List<CellData>> lists = CommonUtil.subList(cellDatas, 40);
			for (List<CellData> list : lists) {
				cellDataDao.batchInsertCellData(annualId, list);
			}
		}
	}

	// 根据cellDataId 修改报表数据
	public void updateCellDataValueById(String annualId, String cellValue, String cellDataId) {
		cellDataDao.updateCellDataValueById(annualId, cellValue, cellDataId);
	}

	/**
	 * 动态创建指标数据表
	 * 
	 * @param annualId
	 */
	public void createTable(String annualId) {
		String tableName = ReportConstant.EXECUTIVE_TABLE_PREFIX + annualId;
		String count = executeSql(ReportConstant.ISEXIST_TABLE_SQL, Sqls.callback.str(),
				new VarParam(null, null, "table_name", tableName)).getString();
		if ("0".equals(count)) {
			executeSql(ReportConstant.EXECUTIVE_CREATE_TABLE_SQL, null,
					new VarParam("table_name", tableName, null, null));
		}
	}

	//根据报表的ID和党组织ID，得到这张报表有多少条汇总数据，主要是为了判断这张报表是不是已汇总了
	public int isHaveSumCellDataByTemplateAndDzzId(String annualId, String dzzId, String templateId) {
		return cellDataDao.isHaveSumCellDataByTemplateAndDzzId(annualId, dzzId, templateId);
	}

	public String getCellValueByTemplateIdColRow(String annualId, String templateId, String col, String row,
			String dzzId) {
		//2015年、2016年的数据特殊处理
		if (annualId.equals("R4GC2MDTLVYEGDI6") || annualId.equals("AZMF1FRWUDI40UUJ")) {
			List<String> cellValueList = new ArrayList<String>();
			cellValueList = cellDataDao.getCellValueByTemplateIdColRow(annualId, dzzId, templateId, col, row);
			if (cellValueList.size() == 0) {
				return "0";
			} else {
				return cellValueList.get(0);
			}
		} else {
			//判断当前党组织是汇总单位还是非汇总单位
			RptUnit rptUnit = rptUnitService.queryByDzzIdAndAnnualId(dzzId, annualId);
			List<String> cellValueList = new ArrayList<String>();
			if (null == rptUnit) {
				return "0";
			} else {
				if (rptUnit.getUnitType().equals("1")) {
					cellValueList = cellDataDao.getCellValueByTemplateIdColRow(annualId, dzzId, templateId, col, row);
				} else {
					cellValueList = cellDataDao.getCellValueByTemplateIdColRowSummary(annualId, dzzId, templateId, col,
							row);
				}
				if (cellValueList.size() == 0) {
					return "0";
				} else {
					return cellValueList.get(0);
				}
			}
		}

	}

	/**
	 * 专门针对数据汇总，批量插入指标值的方法
	 * 
	 * @param annualId  年度id
	 * @param cellDatas 单元格数据集合
	 */
	public void batchInsertCellDataSummary(String annualId, List<CellData> cellDatas) {
		String tableName = ReportConstant.EXECUTIVE_TABLE_PREFIX + annualId.toUpperCase();
		if (cellDatas != null && cellDatas.size() > 0) {
			List<List<CellData>> lists = CommonUtil.subList(cellDatas, 40);
			List<Sql> sqls = new ArrayList<Sql>();
			for (List<CellData> list : lists) {
				StringBuilder batchInsertCellValueSql = new StringBuilder();
				batchInsertCellValueSql.append("INSERT ALL ");

				for (CellData cellData : list) {
					batchInsertCellValueSql.append("INTO ").append(tableName).append(" VALUES (")
							.append(addQuotesStr(cellData.getCellDataId())).append(",")
							.append(addQuotesStr(cellData.getCellId())).append(",")
							.append(addQuotesStr(cellData.getCellValue())).append(",")
							.append(addQuotesStr(cellData.getHistoryCellValue())).append(",")
							.append(addQuotesStr(cellData.getCellType())).append(",").append(cellData.getAbsRowNum())
							.append(",").append(cellData.getAbsColNum()).append(",").append(cellData.getRelRowNum())
							.append(",").append(cellData.getRelColNum()).append(",")
							.append(addQuotesStr(cellData.getDzzId())).append(",")
							.append(addQuotesStr(cellData.getTemplateId())).append(",")
							.append(addQuotesStr(cellData.getSumStatus())).append(",")
							.append(addQuotesStr(cellData.getBatchId())).append(",").append("SYSDATE").append(")  ");
				}
				batchInsertCellValueSql.append(" SELECT * FROM dual");
				try {
					Sql batchInsertsql = Sqls.create(batchInsertCellValueSql.toString());
					sqls.add(batchInsertsql);
				} catch (Exception e) {
					e.printStackTrace();
				}

			}
			dao.execute(sqls.toArray(new Sql[0]));
		}
	}

	//加个单引号给传进来的字符串（专门为数据汇总批量插入方法）
	public String addQuotesStr(String str) {
		return "'" + str + "'";
	}

	//给该张报表的指标赋值为0，只包括1，2，3，即自动生成类，
	public List<CellData> templateIndexAssignmentZero(String templateId, String annualId, String dzzId) {
		List<Cell> cellList = cellService.getCellStrategyByTemplateId(templateId);
		List<CellData> cellDataList = new ArrayList<CellData>();
		for (Cell cell : cellList) {
			if ("1".equals(cell.getCellType()) || "2".equals(cell.getCellType()) || "3".equals(cell.getCellType())) {
				CellData cellData = new CellData(cell, "0", dzzId);
				cellData.setInsertDate(new Date());
				cellDataList.add(cellData);
			}
		}
		return cellDataList;
	}

	//给汇总单位的该张报表的指标赋值为0，只包括1，2，3，即自动生成类，
	public List<CellData> templateIndexAssignmentZeroSummary(String templateId, String annualId, String dzzId) {
		List<Cell> cellList = cellService.getCellStrategyByTemplateId(templateId);
		List<CellData> cellDataList = new ArrayList<CellData>();
		for (Cell cell : cellList) {
			if ("1".equals(cell.getCellType()) || "2".equals(cell.getCellType()) || "3".equals(cell.getCellType())) {
				CellData cellData = new CellData(cell, "0", dzzId);
				cellData.setSumStatus("1");
				cellData.setInsertDate(new Date());
				cellDataList.add(cellData);
			}
		}
		return cellDataList;
	}

	public Map<String, String> getCellValuesForColRow(String annualId, String templateId, String dzzId,
			String unitType) {
		String tableName = "RPT_CELL_DATA_" + annualId;
		String sqlStr = "select max(CELL_VALUE) CELL_VALUE, ABS_ROW_NUM, ABS_COL_NUM from $tableName where DZZ_ID = @dzzId "
				+ " and TEMPLATE_ID = @templateId " + ("1".equals(unitType) ? "" : " and SUM_STATUS = '1' ")
				+ " GROUP BY ABS_ROW_NUM, ABS_COL_NUM ";
		Sql sql = Sqls.create(sqlStr);
		sql.params().set("templateId", templateId).set("dzzId", dzzId);
		sql.vars().set("tableName", tableName);
		sql.setCallback(new SqlCallback() {
			@Override
			public Object invoke(Connection conn, ResultSet rs, Sql sql) throws SQLException {
				Map<String, String> res = new HashMap<>();
				while (rs.next()) {
					String key = rs.getString("ABS_ROW_NUM") + "_" + rs.getString("ABS_COL_NUM");
					res.put(key, rs.getString("CELL_VALUE"));
				}
				return res;
			}
		});
		dao.execute(sql);
		return sql.getObject(Map.class);
	}

	public String getCellValueByTemplateIdColRow(String annualId, String templateId, String col, String row,
			String dzzId, String unitType) {
		List<String> cellValueList = new ArrayList<String>();
		if (unitType.equals("1")) {
			cellValueList = cellDataDao.getCellValueByTemplateIdColRow(annualId, dzzId, templateId, col, row);
		} else {
			cellValueList = cellDataDao.getCellValueByTemplateIdColRowSummary(annualId, dzzId, templateId, col, row);
		}
		if (cellValueList.size() == 0) {
			return "0";
		} else {
			return cellValueList.get(0);
		}
	}

	//删除一个单元格的值
	public void deleteACellValue(String annualId, String templateId, String cellId, String dzzId) {
		cellDataDao.deleteCellDataValueByTemplateIdAndCellId(annualId, templateId, cellId, dzzId);
	}

	//查询是否为非空表
	public int cellValueCountByTemplate(String annualId, String templateId, String dzzId) {
		return cellDataDao.cellDataNotZeroCountByTemplate(annualId, dzzId, templateId);
	}

	public Map<String, Integer> queryValidCellValueCountByDzzid(String annualId, String dzzId) {
		String sql = "select count(1) count, template_id from RPT_CELL_DATA_$annualId cd "
				+ " where cd.dzz_id = @dzzId "
				+ " and cd.cell_type in ('1','2','3') and CELL_VALUE != '0' group by template_id";

		Map<String, Object> vars = new HashMap<String, Object>();
		vars.put("annualId", annualId);
		Map<String, Object> params = new HashMap<String, Object>();
		vars.put("dzzId", dzzId);
		return executeSql(sql, new SqlCallback() {
			@Override
			public Object invoke(Connection conn, ResultSet rs, Sql sql) throws SQLException {
				Map<String, Integer> res = new HashMap<>();
				while (rs.next()) {
					res.put(rs.getString("template_id"), rs.getInt("count"));
				}
				return res;
			}
		}, params, vars).getObject(Map.class);
	}

	/**
	 * 按传入的报表删除传入报表的所有数据
	 * 
	 * @param annualId   年度id
	 * @param dzzId     党组织id
	 * @param templateIdList 报表id
	 */
	public void deleteAllCellDataByDzzIdAndTemplateId(String annualId, String dzzId, List<String> templateIdList) {
		cellDataDao.deleteAllCellDataByDzzIdAndTemplateId(annualId, dzzId, templateIdList);
	}

	/**
	 * 汇总单位数据反查相关方法
	 * 
	 * @param annualId   年度id
	 * @param dzzId     党组织id
	 * @param templateIdList 报表id
	 */
	public Map<String, String> getChildrenCellValues(String annualId, String templateId, String dzzId, String row,
			String col) {
		//2015年、2016年的数据特殊处理
		if (annualId.equals("R4GC2MDTLVYEGDI6") || annualId.equals("AZMF1FRWUDI40UUJ")) {
			List<String> strList = dnDzzService.getAllChildByDzzIdNextLevel(dzzId);
			Map<String, String> mapCellValue = new HashMap<String, String>();
			for (String dzzChildId : strList) {
				List<String> cellValueList = cellDataDao.getCellValueByTemplateIdColRow(annualId, dzzId, templateId,
						col, row);
				if (cellValueList.size() == 0) {
					mapCellValue.put(dzzChildId, "0");
				} else {
					mapCellValue.put(dzzChildId, cellValueList.get(0));
				}
			}
			return mapCellValue;
		} else {
			String tableName = "RPT_CELL_DATA_" + annualId;
			String sqlStr = "select CELL_VALUE,DZZ_ID from $tableName "
					+ " where TEMPLATE_ID = @templateId and ABS_ROW_NUM = @row and ABS_COL_NUM = @col and DZZ_ID in ("
					+ " select DZZID from t_m_zzinfo t where t.SJDZZID = @dzzId and t.DELETEFLAG = '0') ";
			Sql sql = Sqls.create(sqlStr);
			sql.params().set("templateId", templateId).set("row", row).set("col", col).set("dzzId", dzzId);
			sql.vars().set("tableName", tableName);
			sql.setCallback(new SqlCallback() {
				@Override
				public Object invoke(Connection conn, ResultSet rs, Sql sql) throws SQLException {
					Map<String, String> res = new HashMap<>();
					while (rs.next()) {
						String key = rs.getString("DZZ_ID");
						res.put(key, rs.getString("CELL_VALUE"));
					}
					return res;
				}
			});
			dao.execute(sql);
			return sql.getObject(Map.class);
		}
	}
}
